package com.huanwei.mini.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;
import com.huanwei.mini.R;
import com.huanwei.mini.http.API;
import com.huanwei.mini.http.GeRen;
import com.huanwei.mini.utils.JSONUtils;
import com.huanwei.mini.utils.Okhttp;
import com.huanwei.mini.utils.SpfUtils;
import com.huanwei.login_regist.LoginActivity;
import com.huanwei.login_regist.XiuGaiActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 我的
 */
public class My_Fragment extends Fragment {
    View view;

    Unbinder unbinder;
    @BindView(R.id.textView11)
    TextView textView11;
    @BindView(R.id.tv_value_name)
    TextView tvValueName;
    @BindView(R.id.layout_help)
    RelativeLayout layoutHelp;
    @BindView(R.id.tv_xingbie)
    TextView tvXingbie;
    @BindView(R.id.tv_value_xingbie)
    TextView tvValueXingbie;
    @BindView(R.id.textView12)
    TextView textView12;
    @BindView(R.id.tv_value_zhuzhi)
    TextView tvValueZhuzhi;
    @BindView(R.id.layout_about)
    RelativeLayout layoutAbout;
    @BindView(R.id.tv_dianhua)
    TextView tvDianhua;
    @BindView(R.id.tv_value_dianhua)
    TextView tvValueDianhua;
    @BindView(R.id.tv_shenfenzheng)
    TextView tvShenfenzheng;
    @BindView(R.id.tv_value_shenfenzheng)
    TextView tvValueShenfenzheng;
    @BindView(R.id.tv_bumen)
    TextView tvBumen;
    @BindView(R.id.tv_value_bumen)
    TextView tvValueBumen;
    @BindView(R.id.tv_gangwei)
    TextView tvGangwei;
    @BindView(R.id.tv_value_gangwei)
    TextView tvValueGangwei;
    @BindView(R.id.change)
    RoundButton change;
    @BindView(R.id.logout)
    RoundButton logout;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_my, null);
            ButterKnife.bind(this, view);

        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        geren();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();


    }
    public void geren() {
        API.GR(getActivity(), new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("xxx", "xx" + st);
                GeRen geRen = JSONUtils.parseJSON(st, GeRen.class);
                if (geRen.getError().equals("0")) {
                    setGeRen(geRen);
                } else {
                    Toast.makeText(getActivity(), ""+geRen.getMsg(), Toast.LENGTH_SHORT).show();

                }


            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("xxx", "xx" + errst);
            }
        });
    }

    public void setGeRen(GeRen geRen) {
        GeRen.DataBean data = geRen.getData();
        tvValueName.setText(data.getNames());
        tvValueDianhua.setText(data.getIphone());
        tvValueBumen.setText(data.getXm());
        tvValueGangwei.setText(data.getDs());
        tvValueShenfenzheng.setText(data.getMbnumber());
        tvValueZhuzhi.setText(data.getFaddress());
        tvValueXingbie.setText("" + data.getMbsex());
    }

    @OnClick({R.id.change, R.id.logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.change:
                startActivity(new Intent(getActivity(), XiuGaiActivity.class));
                break;
            case R.id.logout:
                SpfUtils.put(getActivity(), "islogin", true);
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
        }
    }
}
